﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Net;

namespace compusUpdates {
    public class Lesson {
        public String code { get; set; }
        public String name { get; set; }

        public int agenda { get; set; }
        public int eggrafa { get; set; }
        public int ergasies { get; set; }
        public int anakoinwseis { get; set; }
        public int forum { get; set; }
        public int description { get; set; }

        public Lesson(String code, String name) {
            this.code = code;
            this.name = name;
            this.agenda = 0;
            this.eggrafa = 0;
            this.ergasies = 0;
            this.anakoinwseis = 0;
            this.forum = 0;
            this.description = 0;
        }

        public void getUpdates() {
            try {
                String refer = "http://compus.uom.gr/index.php";
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://compus.uom.gr/" + this.code + "/index.php");

                request.CookieContainer = Program.Cookies;
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.Referer = refer;
                request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                request.Headers.Add("Accept-Language", "en-us,en;q=0.5");
                request.Headers.Add("Accept-Encoding", "gzip,deflate");
                request.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
                request.Headers.Add("Keep-Alive", "300");
                request.KeepAlive = true;
                request.AllowAutoRedirect = true;

                //Console.Write(request.Headers.ToString());

                Stream dataStream = request.GetRequestStream();
                dataStream.Close();

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                //Console.WriteLine(((HttpWebResponse)response).StatusDescription);
                dataStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(dataStream, Encoding.GetEncoding("iso-8859-7"));
                String responseFromServer = reader.ReadToEnd();

                //if(!logged(responseFromServer)){
                //    Program.login(Program.user, Program.pass);
                //}
                newUpdates(responseFromServer);

                reader.Close();
                dataStream.Flush();
                dataStream.Close();
                response.Close();
            } catch (Exception ex) {
            }
        }

        public bool logged(String html) {
            if (Regex.IsMatch(html, "logout")) {
                return true;
            } else {
                return false;
            }
        }

        public void newUpdates(String html) {
            Match m;
            //agenda
            m = Regex.Match(html, "agenda.gif\" border=\"0\"></a>&nbsp;(.*) <img src=\"http://compus.uom.gr/images/icon_new.gif\" border=\"0\"> , 0");
            if (m.Success) {
                this.agenda = int.Parse(m.Groups[1].Value);
            } else {
                this.agenda = 0;
            }
            //eggrafa
            m = Regex.Match(html, "documents.gif\" border=\"0\"></a>&nbsp;(.*) <img src=\"http://compus.uom.gr/images/icon_new.gif\" border=\"0\"> , 0");
            if (m.Success) {
                this.eggrafa = int.Parse(m.Groups[1].Value);
            } else {
                this.eggrafa = 0;
            }
            //Ergasies
            m = Regex.Match(html, "works.gif\" border=\"0\"></a>&nbsp;(.*) <img src=\"http://compus.uom.gr/images/icon_new.gif\" border=\"0\"> , 0");
            if (m.Success) {
                this.ergasies = int.Parse(m.Groups[1].Value);
            } else {
                this.ergasies = 0;
            }
            //Anakoinwseis
            m = Regex.Match(html, "valves.gif\" border=\"0\"></a>&nbsp;(.*) <img src=\"http://compus.uom.gr/images/icon_new.gif\" border=\"0\"> , 0");
            if (m.Success) {
                this.anakoinwseis = int.Parse(m.Groups[1].Value);
            } else {
                this.anakoinwseis = 0;
            }
            //forum
            m = Regex.Match(html, "forum.gif\" border=\"0\"></a>&nbsp;(.*) <img src=\"http://compus.uom.gr/images/icon_new.gif\" border=\"0\"> , 0");
            if (m.Success) {
                this.forum = int.Parse(m.Groups[1].Value);
            } else {
                this.forum = 0;
            }
            //info
            m = Regex.Match(html, "info.gif\" border=\"0\"></a>&nbsp;(.*) <img src=\"http://compus.uom.gr/images/icon_new.gif\" border=\"0\"> , 0");
            if (m.Success) {
                this.description = int.Parse(m.Groups[1].Value);
            } else {
                this.description = 0;
            }
        }

        public String printUpdates() {
            String temp = "";
            if (this.agenda != 0 || this.anakoinwseis != 0 || this.description != 0 || this.eggrafa != 0 || this.ergasies != 0 || this.forum != 0) {
                if (name.Length > 20) {
                    temp += name.Substring(0, 20) + ": ";
                } else {
                    temp += name + ": ";
                }
                if (this.agenda != 0) { temp += " Ατζέντα:" + this.agenda; }
                if (this.anakoinwseis != 0) { temp += " Ανακοινώσεις:" + this.anakoinwseis; }
                if (this.description != 0) { temp += " Περιγραφή:" + this.description; }
                if (this.eggrafa != 0) { temp += " Έγγραφα:" + this.eggrafa; }
                if (this.ergasies != 0) { temp += " Εργασίες:" + this.ergasies; }
                if (this.forum != 0) { temp += " Συζητήσεις:" + this.forum; }
                temp += "\n";
            }
            return temp;
        }
    }
}
